<?php
namespace App\Export;

use App\Models\RecordOrder;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;

class FilterExport implements FromView
{
	protected $timeBetween;
	
	public function __construct($timeBetween)
	{
		$this->timeBetween = $timeBetween;
	}

	public function view():View
	{
		$data = $this->data($this->timeBetween);
		
		return view('admin.filter-export', ['data' => $data]);
	}

	public function data($timeBetween)
	{
		if (empty($timeBetween)) {
			$start = Carbon::now();
			$end = Carbon::now();
			$timeBetween = [$start->startOfDay(), $end->endOfDay()];
		}
		// 充值金额
		$recordRecharge = FilterExport::recordRecharge($timeBetween);
		// 赠送金额
		$recoredSend = FilterExport::recordRecharge($timeBetween);
		// 余额消费
		$recordBalance = FilterExport::recordBalance($timeBetween);
		// 下单数量
		$recordOrder = FilterExport::recordOrder($timeBetween);
		// 卖出商品数量
		$recordGoods = FilterExport::recordGoods($timeBetween);
		// 存酒数量
		$recordStorage = FilterExport::recoredStorage($timeBetween, 1);
		// 取酒数量
		$recordTakeAway = FilterExport::recoredStorage($timeBetween, 2);
		// 存酒人数
		$recordStorageMan = $recordStorage->sum();
		$recordStorage = $recoredStorage->sum('number');
		// 取酒人数
		$recordTakeAwayMan = $recordTakeAway->sum();
		$recordTakeAway = $recordTakeAway->sum('number');
		// 总营业金额
		$recordPay = FilterExport::recordPay($timeBetween);
		// 采购金额
		$purchaseToMaterial = FilterExport::purchaseToMaterial($timeBetween);
		// 商品销售金额
		$goodsSell = FilterExport::goodsSell($timeBetween);
		// 订房数量
		$recoredRoom = FilterExport::recoredRoom($timeBetween);
	}

	static protected function recoredPay($timeBetween)
	{
		$data = RecordPay::whereBetween('created_at', $timeBetween)
			->get(['id', 'pay_type', 'price', 'created_at']);
		return $data;
	}

	static protected function recordRecharge($timeBetween)
	{
		$data = RecordRecharge::whereBetween('created_at', $timeBetween)
			->get(['id', 'type', 'price', 'created_at']);
		return $data;
	}

	static protected function recordBalance($timeBetween)
	{
		$data = RecordBalance::whereBetween('created_at', $timeBetween)
			->where('type', '<>', '1')
			->get(['id','type', 'price', 'created_at']);
		return $data;
	}

	static protected function recoredOrder($timeBetween)
	{
		$data = RecordOrder::with(['order' => function($q){
			$q->withTrashed()->selectRaw('id, status');
		}])->whereBetween('created_at', $timeBetween)
			->get(['id', 'store_id', 'order_id', 'price', 'created_at']);
		return $data;
	}

	static protected function recordGoods($timeBetween)
	{
		$data = RecordGoods::with(['goods' => function($q){
			$q->withTrashed()->selectRaw('id, name');
		}])->whereBetween('created_at', $timeBetween)
			->where('type', '2')
			->get(['id', 'goods_id', 'number', 'created_at']);
		return $data;
	}

	static protected function recordStorage($timeBetween, $type = 1)
	{
		$data = RecordStorage::whereBetween($timeBetween)
			->where('type', $type)
			->get(['id', 'name', 'number', 'goods_id', 'goods_sku_id']);
		return $data;
	}

	static protected function purchaseToMaterial($timeBetween)
	{
		$data = PurchaseToMaterial::whereBetween('created_at', $timeBetween)
				->with(['recordmaterial' => function($q){
					$q->where('type', 1);// 1采购2消耗
				}])
			    ->selectRaw('Month(created_at) as date, sum(unit_price * number) as price')
			    ->groupBy('date')
			    ->get();
		return $data;
	}

	static protected function recoredRoom($timeBetween)
	{
		$data = recoredRoom::whereBetween('created_at', $timeBetween)
			->with(['order' => function ($q) {
				$q->whereNotIn('status', [1, 5]);
			}])
			->get();
		return $data;
	}

	static protected function goodsSell($timeBetween)
	{
		$data = RecordMaterial::whereBetween('created_at')
			->with(['getUnitPrice' => function ($q) {
				$q->get(['id', 'unit_price']);
			}])
			->where([
				'style' => 2,
				'type' => 2
			])
			->get();
		$price = 0;
		$data->map(function ($item) use ($price){
			$price += $item->getUnitPrice->unit_price * $item->number;
		});

		return $price;
	}
}